package com.lzs.chuku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.entity.Commodity;
import com.lzs.entity.ReviewDetail;
import com.lzs.chuku.feign.BaseRukuFeign;
import com.lzs.chuku.mapper.ReviewDetailMapper;
import com.lzs.chuku.service.ReviewDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@Service
public class ReviewDetailServiceImpl extends ServiceImpl<ReviewDetailMapper, ReviewDetail> implements ReviewDetailService {

    @Autowired
    private BaseRukuFeign baseRukuFeign;

    @Autowired
    private ReviewDetailMapper reviewDetailMapper;
    @Override
    public R addReviewDetail(ReviewDetail reviewDetail) {
        try {
            //  调用商品接口 根据商品编码查询 商品信息
            Commodity commodity1 = baseRukuFeign.selectCommodityByBar(reviewDetail.getCommoditybar());
            reviewDetail.setCommodityid(commodity1.getId());
            reviewDetail.setCommodityname(commodity1.getName());
            Long pid=reviewDetail.getPid();
            Long commodityid = reviewDetail.getCommodityid();
            QueryWrapper<ReviewDetail> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",pid);
            wrapper.eq("commodityid",commodityid);
            ReviewDetail reviewDetail1 = reviewDetailMapper.selectOne(wrapper);
            if (reviewDetail1!=null){
                reviewDetail.setQty(reviewDetail.getQty()+reviewDetail1.getQty());
                reviewDetailMapper.update(reviewDetail,wrapper);
            }else{
                reviewDetailMapper.insert(reviewDetail);
            }
            return R.ok().message("新增成功");
        }catch (RuntimeException e){
            return R.error().message("新增失败");
        }
    }
}
